package Offer.offer2017;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: leiwenkang
 * @date: 2020/6/11
 * @time: 21:30
 */
public class Test01 {
    public static void main(String[] args) {
    }

    public static Map<String, List<Long>> group(long[] array) {
        if (array == null || array.length == 0) {
            return new HashMap<>();
        }

        Map<String, List<Long>> map = new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            String key = generateKey(getIndex(array[i]));
            if (map.containsKey(key)) {
                map.get(key).add(array[i]);
            } else {
                map.put(key, Arrays.asList((array[i])));
            }
        }
        return map;
    }

    private static String generateKey(Long index) {
        if (index == 0) {
            return (long)(Math.pow(10, index)) + "~" + ((long)Math.pow(10, index + 1));
        }
        return (long)(Math.pow(10, index) + 1) + "~" + ((long)Math.pow(10, index + 1));
    }

    private static long getIndex(long num) {
        int count = 0;
        while (num != 0) {
            count++;
            num = num / 10;
        }
        return count;

    }
}
